{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# python中的6种标准数据类型\n",
    "# 列表 数值 字符串 集合 字典 元组\n",
    "a = 0  # int() float()\n",
    "b = \"\" # 字符串 str()\n",
    "c = [] # 列表 list()\n",
    "d = () # 元组 tuple()\n",
    "e = set() # 集合 set()\n",
    "f = {} # 字典 dict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文件操作\n",
    "- 需要掌握的知识点\n",
    "  - 1、打开一个文件的流程\n",
    "  - 2、打开文件的模式和他的特点\n",
    "  - 3、尝试操作文件的打开，写入，读取\n",
    "  - 4、解决文件打开的上下文关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# r read 使用读模式打开一个文件并打印里面的内容\n",
    "f = open(file=\"./小重山.txt\", mode=\"r\", encoding=\"utf-8\")\n",
    "print(f.read())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# w write方式打开一个文件如果文件不存在，会创建，如果文件存在会覆盖里面的内容\n",
    "f = open(file=\"./小重山.txt\", mode=\"w\", encoding=\"utf-8\")\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# a append方式，打开一个文件，没有则创建，会写入到文件的内容末尾\n",
    "f = open(file=\"./小重山.txt\", mode=\"a\", encoding=\"utf-8\")\n",
    "f.write(\"小重山\")\n",
    "print(f.closed) # 判断文件是否关闭并保存\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用上下文解决文件关闭问题\n",
    "with open(file=\"./小重山.txt\", mode=\"r+\", encoding=\"utf-8\") as f:\n",
    "    # print(f.read()) # 将文件读取\n",
    "    # for i in f:\n",
    "    #     print(i)\n",
    "    print(f.closed)\n",
    "print(f.closed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['昨夜寒蛩不住鸣。\\n', '惊回千里梦，已三更。\\n', '起来独自绕阶行。\\n']\n"
     ]
    }
   ],
   "source": [
    "# 文件操作\n",
    "with open(file=\"./小重山.txt\", mode=\"r+\", encoding=\"utf-8\") as f:\n",
    "    # print(f.read(10))  # 打印全部内容，可以指定size 字符数\n",
    "    # print(f.readline(3))  # 打印一整行，可以指定size，来打印一行之内的内容\n",
    "    # print(f.readlines(25)) # 打印所有内容，整合成一个列表，可以指定hint，上入一行\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "26\n",
      "惊回千里梦，已三更。\n",
      "起来独自绕阶行。\n",
      "人悄悄，帘外月胧明。\n",
      "白首为功名，旧山松竹老，阻归程。\n",
      "欲将心事付瑶琴。\n",
      "知音少，弦断有谁听。小重山小重山小重山\n",
      "\n",
      "真是好词\n",
      "\n",
      "真是好词\n",
      "\n",
      "------------------\n",
      "283\n",
      "\n",
      "惊回千里梦，已三更。\n",
      "起来独自绕阶行。\n",
      "人悄悄，帘外月胧明。\n",
      "白首为功名，旧山松竹老，阻归程。\n",
      "欲将心事付瑶琴。\n",
      "知音少，弦断有谁听。小重山小重山小重山\n",
      "\n",
      "真是好词\n",
      "\n",
      "真是好词\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(file=\"./小重山.txt\", mode=\"r+\", encoding=\"utf-8\") as f:\n",
    "    print(f.tell()) # 查看当前指针的位置\n",
    "    f.readline()\n",
    "    print(f.tell())\n",
    "    print(f.read())\n",
    "    print(\"------------------\")\n",
    "    print(f.tell()) #返回的数值不是字符数，而是字节数\n",
    "    f.seek(25)  # 修改当前指针的位置\n",
    "    print(f.read())\n",
    "    f.write(\"\\n真是好词\\n\")\n",
    "    f.seek(0)\n",
    "    f.write(\"abc\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(file=\"./小重山.txt\", mode=\"r+\", encoding=\"utf-8\") as f:\n",
    "    data = [\"a\\n\",\"b\\n\",\"c\\n\"]\n",
    "    f.writelines(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "para = {\n",
    "    \"key\": \"34aeae4137f99584b7392a1fd882a037\",\n",
    "    \"type\": \"hot_video\",\n",
    "    \"size\": 10\n",
    "}\n",
    "r = requests.get(url=\"http://apis.juhe.cn/fapig/douyin/billboard\", params=para)\n",
    "data = r.json()\n",
    "# json.dumps() # 把字典变成json字符串\n",
    "# json.loads() # 把json字符串变成字典\n",
    "with open(\"douyin.json\", \"w\", encoding=\"utf-8\") as f:\n",
    "    json.dump(data, f, indent=\"  \", ensure_ascii=False) # 读就是json.load() indent 格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "('title', '跟大家分享一下，用#小米SU7 帅气“起漂”的小窍门#赛车???? #赛道漂移 #漂移')\n",
      "('share_url', 'https://www.iesdouyin.com/share/video/7394306433350749475/?region=CN&mid=7394306274092911372&u_code=0&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&from_ssr=1')\n",
      "('author', '雷军')\n",
      "('item_cover', 'https://p11-sign.douyinpic.com/tos-cn-i-0813/ocEPGyBIEiA1IACTtAiDA2AAA9uRfePdzzpCgt~c5_300x400.webp?lk3s=98cf21aa&x-expires=1722996000&x-signature=mcX12aDiaUqJ5M3bUfIeWqNqeQY%3D&from=2563711402_large')\n",
      "('hot_value', 52129880)\n",
      "('hot_words', '漂移,赛车,分享,大家,赛道,小米,一下,SU7,帅气,小窍门')\n",
      "('play_count', 77838875)\n",
      "('digg_count', 1203830)\n",
      "('comment_count', 103410)\n",
      "('title', '马路上开碰碰车？男子神操作惊呆前后车。#湖北交警 #法润平安文明相伴')\n",
      "('share_url', 'https://www.iesdouyin.com/share/video/7394030448244051226/?region=CN&mid=7394030362072025866&u_code=0&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&from_ssr=1')\n",
      "('author', '湖北交警')\n",
      "('item_cover', 'https://p3-sign.douyinpic.com/tos-cn-i-dy/09cfac7980394f33b64d76488d9009fd~c5_300x400.webp?lk3s=98cf21aa&x-expires=1722996000&x-signature=YezlhoCgHUQjHWLsBAncEKmaBzU%3D&from=2563711402_large')\n",
      "('hot_value', 49739944)\n",
      "('hot_words', '平安,文明,路上,操作,相伴,马路上,碰碰车,男子,惊呆,前后,湖北,交警,法润')\n",
      "('play_count', 156666558)\n",
      "('digg_count', 902934)\n",
      "('comment_count', 162949)\n",
      "('title', '拜登宣布退出2024年总统大选。')\n",
      "('share_url', 'https://www.iesdouyin.com/share/video/7394247036129774882/?region=CN&mid=7394246941141519158&u_code=0&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&from_ssr=1')\n",
      "('author', '人民日报')\n",
      "('item_cover', 'https://p3-sign.douyinpic.com/tos-cn-i-dy/81e744a971744b3e98b6255add8da12e~c5_300x400.webp?lk3s=98cf21aa&x-expires=1722996000&x-signature=ORac820pDIx%2B7DrXBEImEVIHAQc%3D&from=2563711402_large')\n",
      "('hot_value', 49283279)\n",
      "('hot_words', '20,24,总统,拜登,宣布,退出,2024,总统大选')\n",
      "('play_count', 60899619)\n",
      "('digg_count', 963919)\n",
      "('comment_count', 27700)\n",
      "('title', '东北的猪运往浙江过天津被“热趴”。 #天津消防 #救援名场面 #猪 #权威解读#一车东北的猪过天津被热趴')\n",
      "('share_url', 'https://www.iesdouyin.com/share/video/7394384960704007478/?region=CN&mid=7394384841002715923&u_code=0&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&from_ssr=1')\n",
      "('author', '天津消防')\n",
      "('item_cover', 'https://p3-sign.douyinpic.com/tos-cn-i-dy/1c0c4e52a1de4b699b0e7dc2efcb3066~c5_300x400.webp?lk3s=98cf21aa&x-expires=1722996000&x-signature=karnPH9%2B3S0RFHGXHKohKnCagGI%3D&from=2563711402_large')\n",
      "('hot_value', 46058768)\n",
      "('hot_words', '救援,一车,东北,天津,消防,场面,权威,解读')\n",
      "('play_count', 74648357)\n",
      "('digg_count', 1011107)\n",
      "('comment_count', 138387)\n",
      "('title', '向momo女士道歉，也对喜欢我的粉丝们说一声对不起')\n",
      "('share_url', 'https://www.iesdouyin.com/share/video/7392832429314034963/?region=CN&mid=7392832332127849257&u_code=0&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&from_ssr=1')\n",
      "('author', '空空日记')\n",
      "('item_cover', 'https://p3-sign.douyinpic.com/tos-cn-p-0015/o0ICy9BmpmxO2FgokUMDfgCACGAfAUcGQQbzEJ~c5_300x400.webp?lk3s=98cf21aa&x-expires=1722996000&x-signature=sFcllfG787MQv1Q7Zy9%2F1uA%2BQWk%3D&from=2563711402_large')\n",
      "('hot_value', 43804467)\n",
      "('hot_words', '喜欢,道歉,我的,对不起,momo,女士,粉丝,一声')\n",
      "('play_count', 160692772)\n",
      "('digg_count', 234139)\n",
      "('comment_count', 18166)\n",
      "('title', '虽然从太原跑到榆次玩，才进去一会就清场了，但是最终看到孩子被救过来了，这是所有人都愿意都愿意看到的结果，孩子就是父母的天。愿所有孩子都健康平安快乐的长大。')\n",
      "('share_url', 'https://www.iesdouyin.com/share/video/7394048797829074239/?region=CN&mid=7394048681185397542&u_code=0&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&from_ssr=1')\n",
      "('author', '香姐一家')\n",
      "('item_cover', 'https://p3-sign.douyinpic.com/tos-cn-p-0015/owAwZv6BIipAwIO8qzDlCcSQBF4PoaurSLEYi~c5_300x400.webp?lk3s=98cf21aa&x-expires=1722996000&x-signature=SSB0cFJC1XmMutnEOkiCN9oXjJc%3D&from=2563711402_large')\n",
      "('hot_value', 43572433)\n",
      "('hot_words', '平安,快乐,父母,结果,孩子,来了,长大,虽然,太原,榆次,进去,一会,清场,但是,最终,看到,救过来,这是,所有人,愿意,就是,健康')\n",
      "('play_count', 72503505)\n",
      "('digg_count', 154747)\n",
      "('comment_count', 6542)\n",
      "('title', '教授被曝性骚扰！中国人民大学：举报情况属实，给予王某某开除党籍处分，解除聘用关系！')\n",
      "('share_url', 'https://www.iesdouyin.com/share/video/7394431541813726499/?region=CN&mid=7394431467075881766&u_code=0&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&from_ssr=1')\n",
      "('author', '人民日报')\n",
      "('item_cover', 'https://p3-sign.douyinpic.com/tos-cn-i-dy/96d0aac8979b491daac076560f74c0c6~c5_300x400.webp?lk3s=98cf21aa&x-expires=1722996000&x-signature=Cj5TNZGFfn9xXFwvscsQb2G%2BHms%3D&from=2563711402_large')\n",
      "('hot_value', 42510597)\n",
      "('hot_words', '中国人,大学,关系,给予,教授,骚扰,举报,某某,性骚扰,中国人民大学,情况,属实,开除党籍,处分,解除,聘用')\n",
      "('play_count', 78725413)\n",
      "('digg_count', 1365479)\n",
      "('comment_count', 116442)\n",
      "('title', '毛妹真有股劲儿了#老妹儿')\n",
      "('share_url', 'https://www.iesdouyin.com/share/video/7394074589447474486/?region=CN&mid=7369181285996104486&u_code=0&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&from_ssr=1')\n",
      "('author', '水生.')\n",
      "('item_cover', 'https://p3-sign.douyinpic.com/tos-cn-p-0015/oEIRdaXDLEBtiA2je0QAeLIC3BGDhdARveyf70~c5_300x400.webp?lk3s=98cf21aa&x-expires=1722996000&x-signature=ZnMV%2F%2BJK8nKPr9pcIczmyisMjF8%3D&from=2563711402_large')\n",
      "('hot_value', 39978451)\n",
      "('hot_words', '妹儿,毛妹,真有,劲儿')\n",
      "('play_count', 72782624)\n",
      "('digg_count', 1506791)\n",
      "('comment_count', 56333)\n",
      "('title', '最新！拜登宣布退出2024年总统大选。')\n",
      "('share_url', 'https://www.iesdouyin.com/share/video/7394233387222977829/?region=CN&mid=7394233293077515062&u_code=0&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&from_ssr=1')\n",
      "('author', '央视新闻')\n",
      "('item_cover', 'https://p26-sign.douyinpic.com/tos-cn-i-dy/3e10ee21bb944fa5895596b7dafd9f1a~c5_300x400.webp?lk3s=98cf21aa&x-expires=1722996000&x-signature=3cIasUA3lS7SNXvuzxDAYN3mts0%3D&from=2563711402_large')\n",
      "('hot_value', 39784520)\n",
      "('hot_words', '20,24,总统,最新,拜登,宣布,退出,2024,总统大选')\n",
      "('play_count', 52499789)\n",
      "('digg_count', 667691)\n",
      "('comment_count', 21211)\n",
      "('title', '沈腾即兴对“儿子”加词：你费点心。一整个笑到了！#电影抓娃娃')\n",
      "('share_url', 'https://www.iesdouyin.com/share/video/7394266256267939124/?region=CN&mid=7394266166660844298&u_code=0&titleType=title&did=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&iid=MS4wLjABAAAANwkJuWIRFOzg5uCpDRpMj4OX-QryoDgn-yYlXQnRwQQ&with_sec_did=1&from_ssr=1')\n",
      "('author', '电影抓娃娃')\n",
      "('item_cover', 'https://p9-sign.douyinpic.com/tos-cn-i-dy/723ad06c08d24b2bae2bb8729aed47de~c5_300x400.webp?lk3s=98cf21aa&x-expires=1722996000&x-signature=k3a9xhqJ8%2BhWF751UwO%2BQrds3A8%3D&from=2563711402_large')\n",
      "('hot_value', 38618582)\n",
      "('hot_words', '沈腾,儿子,电影,娃娃,抓娃娃,点心,即兴,加词,你费,整个')\n",
      "('play_count', 57278792)\n",
      "('digg_count', 583790)\n",
      "('comment_count', 14755)\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "with open(\"douyin.json\", \"r\", encoding=\"utf-8\") as f:\n",
    "    data = json.load(f)\n",
    "print(type(data))\n",
    "for i in data[\"result\"]:\n",
    "    for j in i.items():\n",
    "        print(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "{\n",
      "  \"姓名\": \"张三\",\n",
      "  \"年龄\": 18\n",
      "}\n",
      "<class 'dict'>\n",
      "{'姓名': '张三', '年龄': 18}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "dict1 = {\"姓名\": \"张三\", \"年龄\":18}\n",
    "data = json.dumps(dict1, indent=\"  \", ensure_ascii=False)\n",
    "print(type(data))\n",
    "print(data)\n",
    "\n",
    "dict1 = json.loads(data)\n",
    "print(type(dict1))\n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
